// pages/mylove/mylove.js
let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        loveData: null
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        console.log(app.globalData);
        console.log(this.data.loveData);
        wx.request({
            url: 'http://www.kangliuyong.com:10002/findAllLike',
            data: {
                appkey: app.globalData.appkey,
                tokenString: app.globalData.token
            },
            success: res => {
                console.log(res);
                this.setData({
                    loveData: res.data.result
                })
            }
        })
    },
    // 导航栏事件
    // onClickLeft() {
    //     wx.showToast({
    //         title: '点击返回',
    //         icon: 'none'
    //     });
    // },
    // 取消收藏
    del(e) {
        let pid = e.currentTarget.dataset.pid
        let index = e.currentTarget.dataset.index
        let loveData = this.data.loveData
        console.log(pid);
        console.log(index);
        wx.showModal({
            title: '删除商品',
            content: '确定删除',
            success: (res) => {
                if (res.confirm) {
                    console.log('确定删除');
                    wx.request({
                        url: 'http://www.kangliuyong.com:10002/notlike',
                        method: "POST",
                        header: {
                            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                        },
                        data: {
                            appkey: app.globalData.appkey,
                            pid: pid,
                            tokenString: app.globalData.token
                        },
                        success: res => {
                            console.log(res);
                            if (res.data.msg == '已取消收藏') {
                                wx.showToast({
                                    title: '删除成功',
                                })
                                loveData.splice(index, 1)
                                this.setData({
                                    loveData: loveData
                                })
                            }
                        }
                    })
                } else if (res.cancel) {
                    console.log('取消删除');
                }
            }
        })
    },
    //回到首页
    pushHome() {
        wx.switchTab({
            url: '/pages/index/index',
        })
    },
    //跳转菜单页面
    pushMenu() {
        wx.switchTab({
            url: '/pages/menu/menu',
        })
    }
})